package com.recruit.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.recruit.common.common.mybatis.Page;
import com.recruit.common.common.util.PageUtil;
import com.recruit.common.dto.shift.CreateOrUpdateShiftDTO;
import com.recruit.common.model.ShiftDO;
import com.recruit.common.service.ShiftService;
import com.recruit.common.vo.CreatedVO;
import com.recruit.common.vo.DeletedVO;
import com.recruit.common.vo.PageResponseVO;
import com.recruit.common.vo.UpdatedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;


/**
* @author Eval
* @since 2022-04-04
*/
@RestController
@RequestMapping("/recruit/shift")
@PermissionModule(value = "系统内容管理")
public class ShiftController {

    @Autowired
    private ShiftService shiftService;

    /**
     * 新增轮播图
     * @param validator
     * @return
     */
    @Logger(template = "新增轮播图")
    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "新增轮播图")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateShiftDTO validator) {
        shiftService.createShift(validator);
        return new CreatedVO(9000);
    }

    /**
     * 更新轮播图信息
     * @param id
     * @return
     */
    @Logger(template = "更新轮播图")
    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "更新轮播图")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateShiftDTO validator) {
        ShiftDO shiftDO = shiftService.getById(id);
        if (shiftDO == null) {
            throw new NotFoundException(90000);
        }
        shiftService.updateShift(shiftDO, validator);
        return new UpdatedVO(9100);
    }

    /**
     * 根据id删除轮播图
     * @param id
     * @return
     */
    @Logger(template = "删除轮播图")
    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除轮播图")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        ShiftDO shiftDO  = shiftService.getById(id);
        if (shiftDO == null) {
            throw new NotFoundException(90000);
        }
        shiftService.deleteById(shiftDO.getId());
        return new DeletedVO(9200);
    }

    /**
     * 根据id查询轮播图信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ShiftDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        ShiftDO shiftDO = shiftService.getById(id);
        if (shiftDO == null) {
            throw new NotFoundException(90000);
        }
        return shiftDO;
    }

    /**
     * 查询所有轮播图
     * @return
     */
    @GetMapping("")
    public List<ShiftDO> getAll() {
        List<ShiftDO> shiftDOS = shiftService.getAll();
        if (shiftDOS == null || shiftDOS.size() == 0) {
            throw new NotFoundException(91000);
        }
        return shiftDOS;
    }

    /**
     * 分页查询所有轮播图列表
     * @param count
     * @param page
     * @return
     */
    @Logger(template = "查看轮播图列表")
    @GetMapping("/page")
    @GroupRequired
    @PermissionMeta(value = "查看轮播图列表")
    public PageResponseVO<ShiftDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<ShiftDO> pager = new Page<>( page,count);
        IPage<ShiftDO> paging = shiftService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

    /**
     * 查询部分轮播图用于首页展示
     * @return
     */
    @GetMapping("/show")
    public List<ShiftDO> show() {
        List<ShiftDO> shiftDOS = shiftService.show();
        if (shiftDOS == null || shiftDOS.size() == 0) {
            throw new NotFoundException(91000);
        }
        return shiftDOS;
    }
}
